<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="主体名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入主体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册手机号" prop="mobilePhone">
        <el-input
          v-model="queryParams.mobilePhone"
          placeholder="请输入注册手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否会员" prop="memberFlag">
        <el-select v-model="queryParams.memberFlag" placeholder="请选择是否会员" clearable size="small">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="⽤户状态" prop="userStatus">
        <el-select v-model="queryParams.userStatus" placeholder="请选择⽤户状态" clearable size="small">
        <el-option label="正常" :value="1" />
        <el-option label="冻结" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['nongjt:register:add']"-->
<!--        >新增</el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:register:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:register:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nongjt:register:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="主体名称" align="center" prop="companyName" width="200"/>
      <el-table-column label="注册手机号" align="center" prop="mobilePhone" width="120"/>
      <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否会员" align="center" prop="memberFlag" >
        <template  slot-scope="scope">
          <el-tag v-if="scope.row.memberFlag===1" type="success">是</el-tag>
          <el-tag v-if="scope.row.memberFlag===0" type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="信息是否完善" align="center" prop="perfectFlag" width="120">
        <template  slot-scope="scope">
          <el-tag v-if="scope.row.perfectFlag===1" type="success">已完善</el-tag>
          <el-tag v-if="scope.row.perfectFlag===0" type="danger">未完善</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="⽤户状态" align="center" prop="userStatus" >
        <template  slot-scope="scope">
          <el-tag v-if="scope.row.userStatus===1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.userStatus===0" type="danger">冻结</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="信息完善时间" align="center" prop="perfectTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.perfectTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="试用到期时间" align="center" prop="probationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.probationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买会员时间" align="center" prop="memberTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memberTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员过期时间" align="center" prop="memberExpiredTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memberExpiredTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center" prop="userStatus" />
      <el-table-column label="操作" align="center" fixed="right" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nongjt:register:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nongjt:register:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户注册信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="主体名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入主体名称" />
        </el-form-item>
        <el-form-item label="注册手机号" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入注册手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-date-picker clearable disabled
            v-model="form.registerTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否会员" prop="memberFlag">
          <el-radio-group v-model="form.memberFlag">
            <el-radio-button :label="1">是</el-radio-button>
            <el-radio-button :label="0">否</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="信息是否完善" prop="perfectFlag">
          <el-radio-group v-model="form.perfectFlag">
            <el-radio-button :label="1">是</el-radio-button>
            <el-radio-button :label="0">否</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="信息完善时间" prop="perfectTime">
          <el-date-picker clearable
            v-model="form.perfectTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择信息完善时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="试用到期时间" prop="probationTime">
          <el-date-picker clearable
            v-model="form.probationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择试用到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购买会员时间" prop="memberTime">
          <el-date-picker clearable
            v-model="form.memberTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买会员时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="会员过期时间" prop="memberExpiredTime">
          <el-date-picker clearable
            v-model="form.memberExpiredTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择会员过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="⽤户状态" prop="userStatus">
          <el-radio-group v-model="form.userStatus">
            <el-radio-button :label="1">正常</el-radio-button>
            <el-radio-button :label="0">冻结</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegister, getRegister, delRegister, addRegister, updateRegister } from "@/api/nongjt/register";

export default {
  name: "Register",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户注册信息表格数据
      registerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        mobilePhone: null,
        memberFlag: null,
        userStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "主体名称不能为空", trigger: "blur" }
        ],
        mobilePhone: [
          { required: true, message: "注册手机号不能为空", trigger: "blur" }
        ],
        registerTime: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ],
        memberFlag: [
          { required: true, message: "是否会员不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户注册信息列表 */
    getList() {
      this.loading = true;
      listRegister(this.queryParams).then(response => {
        this.registerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyName: null,
        mobilePhone: null,
        password: null,
        registerTime: null,
        memberFlag: null,
        perfectFlag: null,
        perfectTime: null,
        probationTime: null,
        memberTime: null,
        memberExpiredTime: null,
        delFlag: null,
        userStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户注册信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegister(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户注册信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRegister(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegister(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户注册信息编号为"' + ids + '"的数据项？').then(function() {
        return delRegister(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('nongjt/register/export', {
        ...this.queryParams
      }, `用户注册信息_${new Date().getTime()}.xlsx`)
    },
    /**
     *
     校验⼿机号是否可以被注册
     */
   //  checkMobilePhone() {
   //    if(!this.userInfo.mobilePhone) {
   //      showInfo("请输⼊⼿机号")
   //      return
   //    }
   //  getAction(this.urls.checkMobilePhone,
   //    {'mobilePhone': this.userInfo.mobilePhone}).then(res=>{if (res.code !== 200) {
   //    showModal
   //    ('该⼿机号已被注册，请更换⼿机号')
   //        }
   //     })
   // },
  },
};
</script>
